/*************************************************************************
> File Name: Multi-concurrent_servers.c
> Author: hzy
> Created Time: 2023-09-20 17:09:21
> Description:多进程并发服务器
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <errno.h>

int main(int argc, char const *argv[])
{
    // 创建套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        perror("socket");
        return -1;
    }

    // 端口复用
    int optval = 1;
    socklen_t len = sizeof(optval);
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, len);

    // 设置本地信息
    struct sockaddr_in my_addr;
    bzero(&my_addr, sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(10086);
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    // 绑定信息
    int bind_ret = bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr));
    if (bind_ret != 0)
    {
        perror("bind");
        close(sockfd);
        return -1;
    }

    // 监听
    int listen_ret = listen(sockfd, 3);
    if (listen_ret != 0)
    {
        perror("listen");
        close(sockfd);
        return -1;
    }

    // 等待连接
    while (1)
    {
        // 客户端的信息
        struct sockaddr_in client_addr;
        bzero(&client_addr, sizeof(client_addr));
        char client_ip[INET_ADDRSTRLEN];
        socklen_t clientaddr_len = sizeof(client_addr);

        // 等待连接的到来
        int connfd = accept(sockfd, (struct sockaddr *)&client_addr, &clientaddr_len);
        if (connfd < 0)
        {
            perror("accept");
            continue;
        }

        int process_count = 0;
        pid_t pid = fork();
        if (pid == 0)
        {

            printf("子进程号:%d\n", getpid());

            // 转化并打印客户端信息
            printf("----------------------------------\n");
            inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, sizeof(client_ip));
            printf("Connection succeeded!\n");
            printf("client->ip: %s | port: %d\n", client_ip, ntohs(client_addr.sin_port));

            // 接受信息
            char recv_buf[512] = "";
            int ret = 0;
            while ((ret = recv(connfd, recv_buf, sizeof(recv_buf), 0)) > 0)
            {
                send(connfd, recv_buf, sizeof(recv_buf), 0);
                bzero(recv_buf, sizeof(recv_buf));
                if (ret <= 0)
                {
                    // 关闭已连接的套接字
                    close(connfd);
                    exit(0);
                }
            }
        }
        // 回收子进程
        process_count++;
        printf("%d\n", process_count);
        for (int i = 0; i < process_count; i++)
        {
            int ret = waitpid(-1, NULL, WNOHANG);
            if (ret > 0)
            {
                process_count--;
            }
        }
    }

    close(sockfd);
    return 0;
}